<?php
class ModelMessageAdvice extends Model {
	
	public function addAdvice($sender_name,$data=array()){
		$sql = "insert into ".DB_PREFIX."config_advice  set advice='".$data['advice']."',sender_name='".$sender_name."',title='".$data['title']."',date_added=now()";
		$this->db->query($sql);
	}
	
	
	public function getAdvices($data = array()){
		$sql = "select * from  ".DB_PREFIX."config_advice  where 1=1 ";
		$sort_data = array(
			'date_added',
		);	

		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY date_added";	
		}

		if (isset($data['order']) && ($data['order'] == 'ASC')) {
			$sql .= " ASC";
		} else {
			$sql .= " DESC";
		}

		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	

			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}		
		return $this->db->query($sql)->rows;
	}
	
	public function getAdviceReplies($advice_id){
		$sql = "select * from  ".DB_PREFIX."config_advice_reply  where advice_id='".(int)$advice_id."'";
		return $this->db->query($sql)->rows;
	}
}
?>